<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <el-tab-pane label="用户管理">
            <!-- 左侧内容 -->
            <el-row style="height: 60px; margin-top: 20px">
              <el-button
                type="primary"
                icon="el-icon-plus"
                size="small"
                @click="showDialog = true"
              >
                新增角色
              </el-button>
            </el-row>
            <template>
              <el-table style="width: 100%" border="" :data="list">
                <el-table-column
                  type="index"
                  prop="id"
                  label="序号"
                  width="120"
                  align="center"
                >
                </el-table-column>
                <el-table-column
                  prop="name"
                  label="角色名称"
                  width="240"
                  align="center"
                >
                </el-table-column>
                <el-table-column prop="description" label="描述" align="center">
                </el-table-column>
                <el-table-column label="操作" align="center">
                  <!-- 作用域插槽 -->
                  <template slot-scope="{ row }">
                    <el-button
                      type="success"
                      size="small"
                      @click="assignPerm(row.id)"
                    >
                      分配权限
                    </el-button>
                    <el-button
                      type="primary"
                      size="small"
                      @click="editRole(row.id)"
                    >
                      编辑
                    </el-button>
                    <el-button
                      type="danger"
                      size="small"
                      @click="delRole(row.id)"
                    >
                      删除
                    </el-button>
                  </template>
                </el-table-column>
              </el-table>
            </template>
            <!-- 放置分页组件 -->
            <el-row
              type="flex"
              justify="center"
              style="height: 80px"
              align="middle"
            >
              <el-pagination
                layout="prev, pager, next"
                :total="page.total"
                :page-size="page.pagesize"
                :current-page="page.page"
                @current-change="changePage"
              />
            </el-row>
          </el-tab-pane>

          <!-- 右边内容 -->
          <el-tab-pane label="公司信息">
            <el-alert
              title="对公司名称、公司地址、营业执照、公司地区的更新，将使得公司资料被重新审核，请谨慎修改"
              type="info"
              show-icon
              :closable="false"
            />
            <el-form ref="form" label-width="120px" style="margin-top: 50px">
              <el-form-item label="企业名称">
                <el-input
                  v-model="formData.name"
                  disabled
                  style="width: 400px"
                ></el-input>
              </el-form-item>
              <el-form-item label="公司地址">
                <el-input
                  v-model="formData.companyAddress"
                  disabled
                  style="width: 400px"
                ></el-input>
              </el-form-item>
              <el-form-item label="公司电话">
                <el-input
                  v-model="formData.companyPhone"
                  disabled
                  style="width: 400px"
                ></el-input>
              </el-form-item>
              <el-form-item label="邮箱">
                <el-input
                  v-model="formData.mailbox"
                  disabled
                  style="width: 400px"
                ></el-input>
              </el-form-item>
              <el-form-item disabled label="备注">
                <el-input
                  v-model="formData.remarks"
                  style="width: 400px"
                  disabled
                  :rows="3"
                  type="textarea"
                ></el-input>
              </el-form-item>
            </el-form>
          </el-tab-pane>
        </el-tabs>
      </el-card>

      <el-dialog
        :title="roleForm.id ? '编辑角色' : '新增角色'"
        :visible="showDialog"
        width="40%"
        @close="btnCancel"
      >
        <el-form
          ref="formRole"
          label-width="80px"
          :model="roleForm"
          :rules="rules"
        >
          <el-form-item label="角色名称" prop="name">
            <el-input v-model="roleForm.name"></el-input>
          </el-form-item>
          <el-form-item label="角色描述">
            <el-input v-model="roleForm.description"></el-input>
          </el-form-item>
        </el-form>
        <!-- 底部 -->
        <el-row type="flex" justify="center">
          <el-col :span="6">
            <el-button size="small" @click="btnCancel">取消</el-button>
            <el-button size="small" type="primary" @click="btnOk">
              确定
            </el-button>
          </el-col>
        </el-row>
      </el-dialog>

      <el-dialog
        title="分配权限"
        :visible="showPermDialog"
        @close="btnPermCancel"
      >
        <!-- 树 -->
        <el-tree
          ref="permTree"
          :data="permData"
          :props="defaultProps"
          show-checkbox
          :check-strictly="true"
          default-expand-all
          node-key="id"
          :default-checked-keys="selectCheck"
        />
        <!-- 底部 -->
        <el-row slot="footer" type="flex" justify="center">
          <el-col :span="6">
            <el-button type="primary" size="small" @click="btnPermOk">
              确定
            </el-button>
            <el-button size="small" @click="btnPermCancel">取消</el-button>
          </el-col>
        </el-row>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { tranListToTreeData } from '@/utils'
import { getPermissionList } from '@/api/permisson'
import { getRoleList, getCompanyInfo, delRole, getRoleDetail, editRoleDetail, addRole, assignPrem } from '@/api/setting'
import { mapGetters } from 'vuex'
export default {
  data () {
    return {
      selectCheck: [], // 用来记录权限点标识
      RoleId: '', // 记录分配权限的id
      defaultProps: { label: 'name' },
      permData: [],
      showPermDialog: false, // 控制权限弹层显示
      list: [],
      page: {
        // 放置页码及相关数据
        page: 1,
        pagesize: 2,
        total: 0 // 记录总数
      },
      formData: {
        // 公司信息
      },
      showDialog: false,
      roleForm: {
        name: '',
        description: ''
      },
      rules: {
        name: [{ required: true, message: '角色名称不能为空', trigger: 'blur' }]
      }
    }
  },
  computed: {
    ...mapGetters(['companyId'])
  },

  created () {
    this.getRoleList()
    this.getCompanyInfo()
  },

  methods: {
    btnPermCancel () {
      this.selectCheck = []
      this.showPermDialog = false
    },

    btnPermOk () {
      const res = this.$refs.permTree.getCheckedKeys() // 得到所有的权限id
      assignPrem({ id: this.RoleId, permIds: res })
      this.$message.success('分配权限成功')
      this.showPermDialog = false
    },

    async assignPerm (id) {
      const res = await getPermissionList()
      this.permData = tranListToTreeData(res, '0')
      this.RoleId = id
      const { permIds } = await getRoleDetail(id) // 当前角色所有的权限点数据
      this.selectCheck = permIds
      // console.log(permIds)
      this.showPermDialog = true
    },

    async getRoleList () {
      const { rows, total } = await getRoleList(this.page)
      this.page.total = total
      this.list = rows
    },

    changePage (newPage) {
      this.page.page = newPage
      this.getRoleList()
    },

    async getCompanyInfo () {
      this.formData = await getCompanyInfo(this.companyId)
    },

    async delRole (id) {
      try {
        await this.$confirm('确认删除该角色吗？')
        await delRole(id)
        this.getRoleList()
        this.$message.success('删除角色成功')
      } catch (error) {
        console.log(error)
      }
    },

    async editRole (id) {
      this.roleForm = await getRoleDetail(id) // 实现数据回写
      this.showDialog = true
    },

    async btnOk () {
      try {
        await this.$refs.formRole.validate()
        if (this.roleForm.id) {
          await editRoleDetail(this.roleForm) // 更新
        } else {
          await addRole(this.roleForm)
        }

        this.getRoleList()
        this.$message.success('操作成功')
        this.showDialog = false
      } catch (error) {
        console.log(error)
      }
    },

    btnCancel () {
      this.roleForm = {
        name: '',
        description: ''
      }
      this.$refs.formRole.resetFields()
      this.showDialog = false
    }

  }
}
</script>

<style>
</style>
